package com.foreknow.view;

import com.foreknow.bean.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        Login frame = new Login();
        frame.setVisible(true);
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("\u767B\u5F55");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblUsername = new JLabel("username");
        lblUsername.setFont(new Font("宋体", Font.PLAIN, 18));
        lblUsername.setBounds(89, 49, 118, 15);
        contentPane.add(lblUsername);

        textField = new JTextField();
        textField.setBounds(187, 48, 150, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblPassword = new JLabel("password");
        lblPassword.setFont(new Font("宋体", Font.PLAIN, 18));
        lblPassword.setBounds(91, 100, 104, 15);
        contentPane.add(lblPassword);

        passwordField = new JPasswordField();
        passwordField.setBounds(187, 99, 150, 21);
        contentPane.add(passwordField);

        JButton button = new JButton("\u767B\u5F55"); //登录按钮
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //操作
                //调用service层的方法来实现登录的操作
                AdminService service = new AdminServiceImpl();
                //获取到JTextField用户输入的数据
                String username = textField.getText();
                String password = passwordField.getText();
                Admin admin = service.isCheckLogin(username,password);
                if(admin != null) {
                    System.out.println("success："+ admin.getUsername());
//                    int i = 4;
                    //跳转到MainWindow页面
                    MainWindow mainWindow = new MainWindow();
                    //隐藏当前窗口
                    Login.this.dispose();
                }else {
                    System.out.println("error");
                }
            }
        });
        button.setBounds(102, 175, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("\u6CE8\u518C");
        button_1.setBounds(250, 175, 93, 23);
        contentPane.add(button_1);
    }
}

